package java44th.group.controller.admin.log;

import java44th.group.mapper.MarketLogMapper;
import java44th.group.service.admin.log.MarketlogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.misc.Request;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/13 16:14
 * @Package:java44th.group.controller.admin.log
 * @Description:
 * @Version 1.0
 */
@Component
@Aspect //@Component不省略
public class CustomAspect {


    @Autowired
    HttpServletRequest request;

    @Autowired
    MarketlogService marketlogService;

    public static String username;


    // 登录
    // @Pointcut增加在方法上
    // 使用value属性作为切入点表达式
    // 方法名作为切入点的id
    // @Pointcut("execution(* java44th.group.controller.admin.auth.AuthController2..login(..))")
    @Pointcut("execution(* java44th.group.controller.admin.auth.AuthController2.login(..))")
    public void login() {
    }

    // 返回值 作为代理对象执行方法的返回值
    // JoinPoint连接点 → 获得通知执行过程中的一些特殊的值
    @Around("login()")
    public Object login(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        LinkedHashMap linkedHashMaps= (LinkedHashMap) args[0];
        username = (String) linkedHashMaps.get("username");
        marketlogService.insertLog(joinPoint,request,username,"login");
        Object proceed = joinPoint.proceed();
        return proceed;
    }

    // 退出
    @Pointcut("execution(* java44th.group.controller.admin.auth.AuthController2.logout(..))")
    public void logout() {
    }
    @Around("logout()")
    public Object logout(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        marketlogService.insertLog(joinPoint,request,username,"logout");
        return proceed;
    }

    // 更新管理员
    @Pointcut("execution(* java44th.group.controller.admin.admin.ListController.update(..))")
    public void update() {
    }
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        marketlogService.insertLog(joinPoint,request,username,"update");
        return proceed;
    }


    //删除管理员
    @Pointcut("execution(* java44th.group.controller.admin.admin.ListController.delete(..))")
    public void delete() {
    }
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        marketlogService.insertLog(joinPoint,request,username,"delete");
        return proceed;
    }



    //更新管理员
    @Pointcut("execution(* java44th.group.controller.admin.admin.ListController.create(..))")
    public void create() {
    }
    @Around("create()")
    public Object create(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        marketlogService.insertLog(joinPoint,request,username,"create");
        return proceed;
    }

    //退款
    @Pointcut("execution(* java44th.group.controller.admin.order.OrderController.refund(..))")
    public void refund() {
    }
    @Around("refund()")
    public Object refund(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        marketlogService.insertLog(joinPoint,request,username,"refund");
        return proceed;
    }
    //发货
    @Pointcut("execution(* java44th.group.controller.admin.order.OrderController.ship(..))")
    public void ship() {
    }
    @Around("ship()")
    public Object ship(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        marketlogService.insertLog(joinPoint,request,username,"ship");
        return proceed;
    }
    //删除订单
    @Pointcut("execution(* java44th.group.controller.admin.order.OrderController.delete(..))")
    public void deleteOrder() {
    }
    @Around("deleteOrder()")
    public Object deleteOrder(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        marketlogService.insertLog(joinPoint,request,username,"deleteOrder");
        return proceed;
    }
}
